#region Using directives
using System;
using System.Collections;
	
using System.Data ;
using WP.DB;
using WP.MF.DataEntity; 
using WP.Commons;
#endregion

namespace WP.MF.DataAccess
{

	/*
	 * Created by Z
	 * At  Tuesday, August 02, 2005
	 */

    public class MUserBranchDAL
	{
		public MUserBranchDAL()
		{
		}

		#region User Access
		public MUserBranchDEL SelectUserAccess(DBAccess dBAccess,string strBranchNo)
		{
			
			
			MUserBranchDEL mUserBranchDEL=new MUserBranchDEL();
			MUserBranchDEL mUserBranchDEL_Access=new MUserBranchDEL();
			string strSql=string.Empty;

			strSql=@"SELECT UserName,Description  FROM MUser Order By UserName ";
			dBAccess.GetDataSet(strSql,MUserBranchDEL.TABLENAME_MUSERBRANCH,mUserBranchDEL);
			
			//Key
			DataColumn[] DCS=new DataColumn[1];
			DCS[0]=mUserBranchDEL.Tables[MUserBranchDEL.TABLENAME_MUSERBRANCH].Columns[MUserBranchDEL.USERNAME];			
			mUserBranchDEL.Tables[MUserBranchDEL.TABLENAME_MUSERBRANCH].PrimaryKey=DCS;		

			strSql=@"SELECT UserName,Access,GLAccess  FROM MUserBranch where BranchNo='" + BaseFunctions.SpecialCharDeal(strBranchNo) + "' Order By UserName ";
			dBAccess.GetDataSet(strSql,MUserBranchDEL.TABLENAME_MUSERBRANCH,mUserBranchDEL_Access);

			DCS[0]=mUserBranchDEL_Access.Tables[MUserBranchDEL.TABLENAME_MUSERBRANCH].Columns[MUserBranchDEL.USERNAME];			
			mUserBranchDEL_Access.Tables[MUserBranchDEL.TABLENAME_MUSERBRANCH].PrimaryKey=DCS;		

			mUserBranchDEL.Merge(mUserBranchDEL_Access,false);

			return mUserBranchDEL;
			
		}
		#endregion
		
		#region  DeleteUserAccess
		public void DeleteUserAccess(DBAccess dBAccess, string strBranchNo)
		{
			string strSql="";

			strSql="delete from MUserBranch WHERE BranchNo = '" + BaseFunctions.SpecialCharDeal( strBranchNo) +"'";

			dBAccess.ExecuteSql(strSql);
		}
		#endregion

		#region Insert User Access by DataRow
		public void InsertUserAccess(DBAccess dBAccess,MUserBranchDEL mUserBranchDEL)
		{
			string strWholeSql="";
			string strSql=string.Empty;

			for(int i = 0 ; i < mUserBranchDEL.Tables[MUserBranchDEL.TABLENAME_MUSERBRANCH].Rows.Count ; i++)
			{				
				int iIndex=0;
				object[] oValues;

				DataRow DR=mUserBranchDEL.Tables[MUserBranchDEL.TABLENAME_MUSERBRANCH].Rows[i];
				strSql=@"insert into mUserBranch(UserName,BranchNo,Access,GLAccess) values('{0}','{1}',{2},{3});"   ;

				oValues=new object[5];
				oValues[iIndex++]=BaseFunctions.SpecialCharDeal( DR[MUserBranchDEL.USERNAME].ToString());
				oValues[iIndex++]=BaseFunctions.SpecialCharDeal( DR[MUserBranchDEL.BRANCHNO].ToString());				
				if (bool.Parse(DR[MUserBranchDEL.ACCESS].ToString()))
				{
					oValues[iIndex++]=1;
				}
				else
				{
					oValues[iIndex++]=0;
				}
				if (bool.Parse(DR[MUserBranchDEL.GLACCESS].ToString()))
				{
					oValues[iIndex++]=1;
				}
				else
				{
					oValues[iIndex++]=0;
				}				
				
				strSql=string.Format(strSql,oValues);
				strWholeSql +=strSql;
			}
			if (strWholeSql.Length!=0)
			{
				dBAccess.ExecuteSql(strWholeSql);
			}
			
		}
		#endregion Insert User Access

	}

}
